package com.wmwj.hxlj.service.impl;

import com.wmwj.hxlj.entity.Comment;
import com.wmwj.hxlj.entity.User;
import com.wmwj.hxlj.entity.vo.PageResult;
import com.wmwj.hxlj.mapper.ArticleMapper;
import com.wmwj.hxlj.mapper.CommentMapper;
import com.wmwj.hxlj.mapper.UserMapper;
import com.wmwj.hxlj.service.ArticleService;
import com.wmwj.hxlj.service.CommentService;
import com.wmwj.hxlj.service.UserService;
import com.wmwj.hxlj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<Comment> addComment(Comment comment) {
        if (comment.getContent() == null || comment.getContent().trim().isEmpty()) {
            return Result.error("评论内容不能为空");
        }
        //文章不存在
        if(articleMapper.findById(comment.getArticleId()) == null){
            return Result.error("文章不存在");
        }
        //用户不存在
        if(userMapper.findById(comment.getUserId()) == null){
            return Result.error("用户不存在");
        }

        //设置时间
        comment.setCreatedAt(new java.util.Date());
        //添加父评论
        if (comment.getParentId() == null) {
            //设置评论主键未空
            comment.setCommentId(null);
            //插入评论
           int row = commentMapper.insertComment(comment);
           if (row != 0){
               // 2. 更新父评论的回复数量
               commentMapper.incrementReplyCount(comment.getParentId());
               System.out.println("更新父评论的回复数量"+comment.getParentId());
               //更新文章的评论数量
               articleMapper.incrementCommentsCount(comment.getArticleId());
               return Result.success("评论成功", comment);
           }else {
               return Result.error("评论失败");
           }

        }
        // 1. 校验父评论是否存在（如果是子评论）
        if (comment.getParentId() != null) {
            Comment parentComment = commentMapper.selectCommentById(comment.getParentId());
            if (parentComment == null) {
                throw new IllegalArgumentException("父评论不存在");
            }
        }
        // 3. 插入新评论
        int row = commentMapper.insertComment(comment);
        if(row == 0)
            return Result.error("评论失败");
        else{
            // 2. 更新父评论的回复数量
            commentMapper.incrementReplyCount(comment.getParentId());
            //更新文章的评论数量
            articleMapper.incrementCommentsCount(comment.getArticleId());
            return Result.success("评论成功",comment);
        }

    }

    @Override
    public Result<String> deleteComment(Integer commentId) {
        if (commentId == null) {
            return Result.error("评论ID不能为空");
        }
        int row = commentMapper.deleteCommentById(commentId);
        if (row == 0) {
            return Result.error("删除评论失败");
        } else {
            return Result.success("删除评论成功");
        }
    }

    @Override
    public Result<List<Comment>> getCommentsByArticleId(Long articleId) {
        List<Comment> comments = commentMapper.selectCommentsByArticleId(articleId);
        if (comments == null || comments.isEmpty()) {
            return Result.error("该文章没有评论");
        }
        return Result.success(comments);
    }

    @Override
    public Result<List<Comment>> getParentComments(Long articleId) {
        List<Comment> comments = commentMapper.selectParentComments(articleId);
        if (comments == null || comments.isEmpty()) {
            return Result.error("该文章没有评论");
        }
        return Result.success(comments);
    }

    @Override
    public Result<List<Comment>> getChildComments(Long parentId) {
        List<Comment> comments = commentMapper.selectChildComments(parentId);
        if (comments == null || comments.isEmpty()) {
            return Result.error("该评论没有回复");
        }
        return Result.success(comments);
    }

    @Override
    public Result<PageResult<Comment>> getAllComments(int pageStart) {

        //分页查询
        int pageStarts = (pageStart - 1) * 10;
        int pageSize = 10;
        List<Comment> comments = commentMapper.selectAllComments(pageStarts, pageSize);
        //查询总数
        long total = commentMapper.countAllComments();

        PageResult<Comment> pageResult = new PageResult<>(comments,total);

        return Result.success(pageResult);

    }

    @Override
    public Result<PageResult<Comment>> adminSearchComments(String keyword, Integer pageStart) {

        int pageStarts = (pageStart - 1) * 10;
        int pageSize = 10;

        List<Comment> comments = commentMapper.adminSearchComments(keyword, pageStarts, pageSize);
        if (comments == null || comments.isEmpty()) {
            return Result.error("没有找到相关评论");
        }
        PageResult<Comment> pageResult = new PageResult<>(comments, commentMapper.adminCountByKeyword(keyword));
        return Result.success(pageResult);

    }

    @Override
    public Result<String> likeComment(Long commentId) {
        if (commentId == null) {
            return Result.error("该评论不存在，请重新加载页面。");
        }
        int row = commentMapper.incrementLikes(commentId);
        if (row == 0) {
            return Result.error("点赞评论失败");
        } else {
            return Result.success("点赞评论成功");
        }
    }

}